package com.songlanyun.modules.pointgiveaway.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.utils.DateUtils;
import com.songlanyun.modules.pointconfig.entity.PointConfigLog;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;

import com.songlanyun.modules.pointgiveaway.dao.PointGiveAwayDao;
import com.songlanyun.modules.pointgiveaway.entity.PointGiveAwayEntity;
import com.songlanyun.modules.pointgiveaway.service.PointGiveAwayService;


@Service("pointGiveAwayService")
public class PointGiveAwayServiceImpl extends ServiceImpl<PointGiveAwayDao, PointGiveAwayEntity> implements PointGiveAwayService {

    @Override
    public PointGiveAwayEntity create(PointGiveAwayEntity entity) {
         this.baseMapper.insert(entity);
         return entity;
    }

    @Override
    public PointGiveAwayEntity modify(PointGiveAwayEntity entity) {
        return null;
    }

    @Override
    public void remove(Long id) {

    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        LambdaQueryWrapper<PointGiveAwayEntity> wrapper = new LambdaQueryWrapper<>();
        String giveRealName = MapUtil.getStr(params, "give_real_name");
        String giveMobile = MapUtil.getStr(params, "give_mobile");

        String acceptRealName = MapUtil.getStr(params, "accept_real_name");
        String acceptMobile = MapUtil.getStr(params, "accept_mobile");

        Date startTime = MapUtil.getDate(params, "start_time");
        Date endTime = MapUtil.getDate(params, "end_time");

        wrapper.like(StringUtils.isNotBlank(giveRealName),PointGiveAwayEntity::getGiveRealName,giveRealName);
        wrapper.like(StringUtils.isNotBlank(giveMobile),PointGiveAwayEntity::getGiveMobile,giveMobile);

        wrapper.like(StringUtils.isNotBlank(acceptRealName),PointGiveAwayEntity::getAcceptRealName,acceptRealName);
        wrapper.like(StringUtils.isNotBlank(acceptMobile),PointGiveAwayEntity::getAcceptMobile,acceptMobile);

        wrapper.ge(ObjectUtil.isNotNull(startTime),PointGiveAwayEntity::getCreateTime,startTime);

        if((ObjectUtil.isNotNull(endTime)))
        {
            endTime = DateUtils.addDateDays(endTime, 1);
            wrapper.le(ObjectUtil.isNotNull(endTime),PointGiveAwayEntity::getCreateTime,endTime);
        }

        wrapper.orderByDesc(PointGiveAwayEntity::getCreateTime);
        IPage<PointGiveAwayEntity> page = this.page(
                new Query<PointGiveAwayEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public BigDecimal allHandlingFee() {
        BigDecimal all  = this.baseMapper.allHandlingFee();
        return all;
    }

}
